package com.tulingxueyuan.sentinelnew.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author :tangweiliang
 * @date : 2023/1/11 11:35
 * 未来的你会感谢现在努力的自己
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
        System.out.println("下单成功!");
        // 填写另一个服务的路径(url) 以及参数类型
        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        return "Hello World!"+msg;
    }
    /**
     * 测试过滤器工厂 添加请求头 X‐Request‐color,red
     */
    @RequestMapping("/header")
    public String Header(@RequestHeader("X-Request-color") String color){
        return color;
    }

    @RequestMapping("/flow")
    //@SentinelResource(value = "flow",blockHandler = "blockHandlerFlow")
    public String flow(){
        return "正常访问";
    }

    @RequestMapping("/flowThread")
    //@SentinelResource(value = "flowThread",blockHandler = "blockHandlerFlow")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("正常访问");
        return "正常访问";
    }

    @RequestMapping("/get")
    public String get() throws InterruptedException {
        return "查询订单";
    }

    @RequestMapping("/err")
    public String err(){
        int i = 1/0;
        return "hello";
    }

    /**
     * 热点规则，必须使用@SentinelResource
     * @param id
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/get/{id}")
    public String getById(@PathVariable("id") Integer id) throws InterruptedException {
        System.out.println("正常访问!");
        return "正常访问";
    }
}
